<?php
if (!defined("MCTDESIGN")) die("# ");
switch($act){
	case "category":
	   $title = getTitle(1);
	   $content = show_list_tour();
       break;
   case "place":
	   $title = getTitle(3);
	   $content = show_list_tour_place();
       break;  
   case "calendar":
	   $title = "Lịch khởi hành";
	   $content = show_list_tour_calendar();
       break;      
   case "details":
	   $title = getTitle(2);
	   $content = show_tour();
       break;   
   default : 
	   $content = msg_Error("<br><br><br>Không tìm thấy trang này ");
	   break;     
}
include './_classes/mailer/senmail.php';
if(!empty($_GET['send_dat_tour']) && $_GET['send_dat_tour'] == "true"){
    global $anyDB, $tb_fix;
    $id = $_GET['id'];
    $name = $_GET['name'];
    $email = $_GET['email'];
    $phone = $_GET['phone'];
    $ngaykh = $_GET['ngaykh'];
    $diachi = $_GET['diachi'];
    $quoctich = $_GET['quoctich'];
    $nguoilon = $_GET['nguoilon'];
    $treem = $_GET['treem'];
    $yeucauthem = $_GET['yeucauthem'];
    $tieude = "Liên hệ đặt Tour";
    $sql = "select * from ".$tb_fix."_tour as t left join ".$tb_fix."_tour_menu as c on t.tou_category = c.tme_id   where t.tou_id = $id ";
    $q = $anyDB->query($sql);
    $arr = $anyDB->getNext($q);
    if($arr['tme_root_id'] == 1){
        $rootName = "tour-nuoc-ngoai";
    }else{
        $rootName = "tour-trong-nuoc";
    }    
    /*$link = "".get_data('shop_url','config_values')."tour/".$arr['tme_root_id']."/".$rootName."/".$arr['tme_id']."/".convertText($arr['tme_title'])."/".$arr['tou_id']."/".convertText($arr['tou_title']).".html";
	$subject = "Thông tin đặt tour  ";		
    $mess = "";
    $mess .= "<div style=\"width: 100%;height: 25px;background: green; text-align: center;color: #ffffff;\"> <span style='display: block;font-weight: bold;padding-top: 1px !important;' ><strong>Thông tin đặt tour  </strong></span></div>\n"; 
    $mess .= "    <div style=\"padding-top: 3px;\">Xin chào Administrator: <strong> Có 1 khách hàng đặt Tour tại website http://manhlong.vn, thời gian gửi thông tin là ".gmdates(time())." </strong></div>\n";
    $mess .= "<ul style=\"padding-left: 25px;position: relative;\">\n";     
    $mess .= "    <li style=\"padding-top: 3px;\">Họ tên: <strong> $name</strong></li>\n"; 
    $mess .= "    <li style=\"padding-top: 3px;\">Email : <strong> $email</strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Điện thoại: <strong> $phone</strong></li>\n"; 
    $mess .= "    <li style=\"padding-top: 3px;\">Địa chỉ: <strong> $diachi</strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Quốc tịch: <strong> $quoctich</strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Tên tour: <strong> <a href='$link' target='_blank' title='Click để xem chi tiết tour'> ".$arr['tou_title']." </a></strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Số người lớn: <strong> $nguoilon</strong></li>\n"; 
    $mess .= "    <li style=\"padding-top: 3px;\">Số trẻ em : <strong> $treem</strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Ngày muốn khởi hành:  <strong> $ngaykh</strong></li>\n";   
    $mess .= "    <li style=\"padding-top: 3px;\">Yêu cầu của khách hàng:  <strong> $yeucauthem</strong></li>\n";                           
    $mess .= "</ul>\n";         
    $send = sendlienhe($subject,$tieude,$email,$mess);
    // THEM DAT TOUR VAO ADMIN
    if($send == 1){
        $sql = "insert into ".$tb_fix."_tour_order values (0,'{$arr['tou_id']}','".time()."',0,'$name','$email','$phone','$diachi','$quoctich','$nguoilon','$treem','$ngaykh','$yeucauthem')";
        $anyDB->query($sql);
    }
    echo $send;    
    exit();*/
    $sql = "insert into ".$tb_fix."_tour_order values (0,'{$arr['tou_id']}','".time()."',0,'$name','$email','$phone','$diachi','$quoctich','$nguoilon','$treem','$ngaykh','$yeucauthem')";
    $q = $anyDB->query($sql);
    if($q){
        echo 1;
    }else{
        echo 0;
    }
    exit();
}

function getTitle($num){
    global $anyDB,$tb_fix;
    $tit = "";
    if($num == 1){
        $rootid = (int)$_GET['rootId'];
        $catid = (int)$_GET['catId'];
        if($catid){
            $sql = "select tme_title from ".$tb_fix."_tour_menu where tme_id = '$catid'";
            $q = $anyDB->query($sql);
            $r = $anyDB->getNext($q);
            $tit = $r['tme_title'];    
        }else{
            if($rootid){
                if($rootid == 1){
                    $tit = "Tours Nước Ngoài";
                }else{
                    $tit = "Tours Trong Nước";
                }
            }
        }
    }
    if($num == 2){
        $id = (int)$_GET['id'];
        $sql = "select tou_title from ".$tb_fix."_tour where tou_id = '$id'";
        $q = $anyDB->query($sql);
        $r = $anyDB->getNext($q);
        $tit = $r['tou_title'];
    }
    if($num == 3){
        $id = (int)$_GET['placeId'];
        $sql = "select tkh_name from ".$tb_fix."_tour_khoihanh where tkh_id = '$id'";
        $q = $anyDB->query($sql);
        $r = $anyDB->getNext($q);
        $tit = $r['tkh_name'];
    }
    return $tit;
}
function get_parent_names($cat_id, &$str) {

	global $mysql, $tb_fix, $stop, $dir_tpl;

	$query = $mysql->query("SELECT * FROM ".$tb_fix."_tour_menu WHERE tme_id='$cat_id' ORDER BY tme_order ASC");

	while ($result = $mysql->fetch_array($query)){

		if ( $cat_id == $result['tme_id'] ){

			get_parent_names($result['tme_parent_id'],  $str);
            $caturl = convertText($result['tme_title']);
			//$str .= ' <img src="'.$dir_tpl.'/images/di_toi.gif">  <a class="active" href="Tours/'.$result['tme_parent_id'].'/'.$caturl.'/'.$result['tme_id'].'/">'. $result['tme_title'] .'</a>';
            if($result['tme_root_id'] == 1){
                $url = "tour/1/tour-nuoc-ngoai/".$result['tme_id']."/$caturl/";
            }else{
                $url = "tour/2/tour-trong-nuoc/".$result['tme_id']."/$caturl/";
            }
            $str .= ' <img src="'.$dir_tpl.'/images/right.gif"> <a href="'.$url.'">'. $result['tme_title'] .'</a>';
		}

	}

}
function show_tour(){
    global $anyDB, $tb_fix, $temp, $dir_tpl;
    $rootId = (int)$_GET['rootId'];
    $catId = (int)$_GET['catId'];;
    $Id = (int)$_GET['id'];
    $temp->assign( 'base_tpl', "".$dir_tpl."" );
    // tour cat name
    get_parent_names($catId, $parent_name);
	$temp->assign( 'parent_name', "$parent_name" );
    // tour cat name
    $temp->assign("RootCate",returnRootName($rootId));
    $sql = "select t.*,tm.tme_title,tk.tkh_name from ".$tb_fix."_tour as t
                    left join ".$tb_fix."_tour_menu as tm
                        on t.tou_category = tm.tme_id
                    left join ".$tb_fix."_tour_khoihanh as tk
                        on tk.tkh_id = t.tkh_id    
                    where 
                        tou_category = '$catId' and tou_root_cat_id = '$rootId' and tou_id = '$Id'
                    limit 0,1";
                   // echo $sql;
    $q = $anyDB->query($sql);
    $r = $anyDB->getNext();
    $tour_cat_parent_id = $r['tour_cat_parent_id'];
    $tour_id = $r['tou_id'];
    $r['tit_url'] = convertText($r['tou_title']);
    $r['tourTimePost'] = gmdates($r['tou_time']);
    $r['tourPrice'] = priceFormat($r['tou_price'],'.');
    // LẤY HÌNH ẢNH TOUR
    $sql = "select mti_url from ".$tb_fix."_tour_img where tou_id = $tour_id order by RAND()";
    $q = $anyDB->query($sql);
    if($anyDB->numRows($q)>0){
        $rImg = $anyDB->getAll($q);
        foreach($rImg as $key=>$val){
            $r['images'][] = $val['mti_url'];    
        }
        
    };
    $temp->assign("row",$r);
    //TOUR CUNG LOAI 
    $sql = "select t.*,tm.tme_title from ".$tb_fix."_tour as t 
                    left join ".$tb_fix."_tour_menu as tm 
                    on t.tou_category = tm.tme_id 
                    where t.tour_cat_parent_id = '$tour_cat_parent_id' and t.tou_id <> '$tour_id' 
                    order by t.tou_id desc limit 0,10";
    $q = $anyDB->query($sql);
    $o_tour = $anyDB->getAll($q);
    foreach($o_tour as $key=>$val){
        $o_tour[$key]['tme_title'] = convertText($val['tme_title']);
        $o_tour[$key]['tourURL'] = convertText($val['tou_title']);
        $o_tour[$key]['tourTimePost'] = gmdates($val['tou_time']);
        $o_tour[$key]['tou_cat_url'] = convertText($val['tme_title']);
        $o_tour[$key]['TourRootName'] = RootTourName($val['tou_root_cat_id']);
        
    }
    $temp->assign("other",$o_tour);
    $content = $temp->fetch( 'modules/_tours_details.mct' );
    return $content;
}
function RootTourName($id){
    if($id == 1){
        return "tour-nuoc-ngoai";
    }else{
        return "tour-trong-nuoc";
    }
}
function returnRootName($id){
    $root = array();
    $root['id'] = $id;
    if($id == 1){
        $root['name'] = "<a href='tour/$id/tour-nuoc-ngoai/'>Tour Nước Ngoài</a>";
    }else{
        $root['name'] = "<a href='tour/$id/tour-trong-nuoc/'>Tour Trong Nước</a>";
    }
    return $root;
}
function show_list_tour(){
    global $anyDB, $tb_fix, $temp, $dir_tpl;
    $rootId = (int)$_GET['rootId'];
    $catId = (int)$_GET['catId'];;
	$temp->assign( 'base_tpl', "".$dir_tpl."" );
    // tour cat name
    $temp->assign("RootCate",returnRootName($rootId));
    // tour cat name
    get_parent_names($catId, $parent_name);
	$temp->assign( 'parent_name', "$parent_name" );
    // show Tour
 // phân trang
    $pagegroup_size = 10;
    $page=_POST('p');
    if (!$page||$page==0){$page=1;}
    $limit=_POST('limit');
    if($limit==''||$limit>5) {$limit=14;}
    $offset=($page-1)*$limit;
    $li="limit $offset,$limit";
    $where = "";
    if($catId) {
        if($rootId == 0){
           $where .= " t.tou_category = '$catId' or t.tour_cat_parent_id = '$catId'"; 
        }else{
            $where .= " t.tou_category = '$catId' and t.tou_root_cat_id = '$rootId'"; 
        }
          
    }else if($rootId){
        $where .= " t.tou_root_cat_id = '$rootId'";
    }
    else {
        $where.= " 1=1";   
    }
     $sql = "select t.tou_id from ".$tb_fix."_tour as t
                    left join ".$tb_fix."_tour_menu as tm
                        on t.tou_category = tm.tme_id
                    where 
                        $where
                     ";
    $q = $anyDB->query($sql);
    $n = $anyDB->numRows($q);
    $totalRecord = $n; // tong so ban ghi
    $sql = "select t.tou_id, t.tou_title,t.tou_code, t.tou_category, t.tour_cat_parent_id,t.tou_root_cat_id,
                t.tou_phuongtien, t.tkh_id, t.tou_desc, t.tou_price, t.tou_begin_time,tm.tme_title 
                from ".$tb_fix."_tour as t
                    left join ".$tb_fix."_tour_menu as tm
                        on t.tou_category = tm.tme_id
                    where 
                        $where
                     order by 
                        tou_id desc $li";                
    $q = $anyDB->query($sql);
    $rows = $anyDB->getAll($q);
    foreach($rows as $k => $v){
        $rows[$k]['tou_title'] = subCutString($v['tou_title'],17);
        $rows[$k]['tou_title_url'] = convertText($v['tou_title']);
        $rows[$k]['tou_cat_url'] = convertText($v['tme_title']);
        $rows[$k]['TourRootName'] = RootTourName($v['tou_root_cat_id']);
        $tourid = $v['tou_id'];
        $sql = "select mti_url from ".$tb_fix."_tour_img where tou_id = $tourid order by RAND() limit 0,1";
        $q = $anyDB->query($sql);
        if($anyDB->numRows($q)>0){
            $rImg = $anyDB->getAll($q);
            foreach($rImg as $key=>$val){
                $rows[$k]['images'] = $val['mti_url'];    
            }
            
        };
    }
    $temp->assign("rows",$rows);    
    $catNameURL = convertText($rCatName['tme_title']);
    $getURL = getURLPaging();
    $pages = pagenavigator($page,$totalRecord,$limit,$pagegroup_size,"$getURL");
    $temp->assign('pages',$pages);                    
	$content = $temp->fetch( 'modules/_tours_list.mct' );
    return $content;
}
function show_list_tour_place(){
    global $anyDB, $tb_fix, $temp, $dir_tpl;
    $placeid = (int)$_GET['placeId'];
	$temp->assign( 'base_tpl', "".$dir_tpl."" );
    // tour cat name
    $temp->assign("RootCate",getTitle(3));
    // show Tour
 // phân trang
    $pagegroup_size = 10;
    $page=_POST('p');
    if (!$page||$page==0){$page=1;}
    $limit=_POST('limit');
    if($limit==''||$limit>5) {$limit=14;}
    $offset=($page-1)*$limit;
    $li="limit $offset,$limit";
    $where = "t.tkh_id = $placeid";    
    $sql = "select t.tou_id from ".$tb_fix."_tour as t
                    left join ".$tb_fix."_tour_menu as tm
                        on t.tou_category = tm.tme_id
                    left join ".$tb_fix."_tour_khoihanh as kh
                        on t.tkh_id = kh.tkh_id
                    where 
                        $where
                     ";
    $q = $anyDB->query($sql);
    $n = $anyDB->numRows($q);
    $totalRecord = $n; // tong so ban ghi
    $sql = "select t.tou_id, t.tou_title,t.tou_code, t.tou_category, t.tour_cat_parent_id,t.tou_root_cat_id,
                t.tou_phuongtien, t.tkh_id, t.tou_desc, t.tou_price, t.tou_begin_time,tm.tme_title 
                from ".$tb_fix."_tour as t
                    left join ".$tb_fix."_tour_menu as tm
                        on t.tou_category = tm.tme_id
                    left join ".$tb_fix."_tour_khoihanh as kh
                        on t.tkh_id = kh.tkh_id     
                    where 
                        $where
                     order by 
                        t.tou_id desc $li";                
    $q = $anyDB->query($sql);
    $rows = $anyDB->getAll($q);
    foreach($rows as $k => $v){
        $rows[$k]['tou_title'] = subCutString($v['tou_title'],17);
        $rows[$k]['tou_title_url'] = convertText($v['tou_title']);
        $rows[$k]['tou_cat_url'] = convertText($v['tme_title']);
        $rows[$k]['TourRootName'] = RootTourName($v['tou_root_cat_id']);
        $tourid = $v['tou_id'];
        $sql = "select mti_url from ".$tb_fix."_tour_img where tou_id = $tourid order by RAND() limit 0,1";
        $q = $anyDB->query($sql);
        if($anyDB->numRows($q)>0){
            $rImg = $anyDB->getAll($q);
            foreach($rImg as $key=>$val){
                $rows[$k]['images'] = $val['mti_url'];    
            }
            
        };
    }
    $temp->assign("rows",$rows);    
    $catNameURL = convertText($rCatName['tme_title']);
    $getURL = getURLPaging();
    $pages = pagenavigator($page,$totalRecord,$limit,$pagegroup_size,"$getURL");
    $temp->assign('pages',$pages);                    
	$content = $temp->fetch( 'modules/_tours_place.mct' );
    return $content;
}
function show_list_tour_calendar(){
    global $anyDB, $tb_fix, $temp, $dir_tpl;
    $rootId = (int)$_GET['rId'];
	$temp->assign( 'base_tpl', "".$dir_tpl."" );
    // tour cat name
    if($rootId == 1) $_breakcum = "Lịch khởi hành: Tour Nước Ngoài"; else $_breakcum = "Lịch khởi hành: Tour Trong Nước";
    $temp->assign("RootCate","$_breakcum");
    // show Tour
 // phân trang
    $pagegroup_size = 10;
    $page=_POST('p');
    if (!$page||$page==0){$page=1;}
    $limit=_POST('limit');
    if($limit==''||$limit>5) {$limit=14;}
    $offset=($page-1)*$limit;
    $li="limit $offset,$limit";
    $where = "t.tou_root_cat_id = $rootId";    
    $sql = "select t.tou_id from ".$tb_fix."_tour as t
                    left join ".$tb_fix."_tour_menu as tm
                        on t.tou_category = tm.tme_id
                    left join ".$tb_fix."_tour_khoihanh as kh
                        on t.tkh_id = kh.tkh_id
                    where 
                        $where
                        and t.tou_calendar != ''
                     ";
    $q = $anyDB->query($sql);
    $n = $anyDB->numRows($q);
    $totalRecord = $n; // tong so ban ghi
    $sql = "select t.tou_id, t.tou_title,t.tou_code, t.tou_category, t.tour_cat_parent_id,t.tou_root_cat_id,
                t.tou_phuongtien, t.tkh_id, t.tou_desc, t.tou_price, t.tou_begin_time,tm.tme_title,t.tou_calendar 
                from ".$tb_fix."_tour as t
                    left join ".$tb_fix."_tour_menu as tm
                        on t.tou_category = tm.tme_id
                    left join ".$tb_fix."_tour_khoihanh as kh
                        on t.tkh_id = kh.tkh_id     
                    where 
                        $where
                        and t.tou_calendar != ''
                     order by 
                        t.tou_id desc $li";                
    $q = $anyDB->query($sql);
    $rows = $anyDB->getAll($q);
    foreach($rows as $k => $v){
        $rows[$k]['tou_title'] = subCutString($v['tou_title'],17);
        $rows[$k]['tou_title_url'] = convertText($v['tou_title']);
        $rows[$k]['tou_cat_url'] = convertText($v['tme_title']);
        $rows[$k]['TourRootName'] = RootTourName($v['tou_root_cat_id']);
        $tourid = $v['tou_id'];
        $sql = "select mti_url from ".$tb_fix."_tour_img where tou_id = $tourid order by RAND() limit 0,1";
        $q = $anyDB->query($sql);
        if($anyDB->numRows($q)>0){
            $rImg = $anyDB->getAll($q);
            foreach($rImg as $key=>$val){
                $rows[$k]['images'] = $val['mti_url'];    
            }
            
        };
    }
    $temp->assign("rows",$rows);    
    $catNameURL = convertText($rCatName['tme_title']);
    $getURL = getURLPaging();
    $pages = pagenavigator($page,$totalRecord,$limit,$pagegroup_size,"$getURL");
    $temp->assign('pages',$pages);                    
	$content = $temp->fetch( 'modules/_tours_calendar.mct' );
    return $content;
}
function getURLPaging(){
    $getURL = $_SERVER['REQUEST_URI'];
    $p = $_GET['p'];
    $newURL = str_replace("&p=$p","",$getURL);
    return $newURL;
}
?>